package com.aidex.biz.controller;

import com.aidex.biz.domain.XiansuoChengbenLog;
import com.aidex.biz.domain.vo.GuanggaoChengBen;
import com.aidex.biz.service.XiansuoChengbenLogService;
import com.aidex.common.annotation.Log;
import com.aidex.common.core.controller.BaseController;
import com.aidex.common.core.domain.R;
import com.aidex.common.core.page.PageDomain;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.SecurityUtils;
import com.aidex.common.utils.poi.ExcelUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 线索成本记录Controller
 *
 * @author sean
 * @email 2074844820@qq.com
 * @date 2025-03-24
 */
@RestController
@RequestMapping("/biz/bizXiansuoChengbenLog")
public class XiansuoChengbenLogController extends BaseController {
    @Autowired
    private XiansuoChengbenLogService xiansuoChengbenLogService;

    /**
     * 查询线索成本记录列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizXiansuoChengbenLog:list')")
    @GetMapping("/list")
    public R<PageInfo> list(XiansuoChengbenLog xiansuoChengbenLog, HttpServletRequest request, HttpServletResponse response) {
        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            xiansuoChengbenLog.setCompanyId(null);
        } else {
            xiansuoChengbenLog.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        xiansuoChengbenLog.setPage(new PageDomain(request, response));
        return R.data(xiansuoChengbenLogService.findPage(xiansuoChengbenLog));
    }

    /**
     * 获取线索成本记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizXiansuoChengbenLog:query')")
    @GetMapping(value = "/{id}")
    public R<XiansuoChengbenLog> detail(@PathVariable("id") String id) {
        return R.data(xiansuoChengbenLogService.get(id));
    }

    /**
     * 新增线索成本记录
     */
    @PreAuthorize("@ss.hasPermi('biz:bizXiansuoChengbenLog:add')")
    @Log(title = "线索成本记录", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated XiansuoChengbenLog xiansuoChengbenLog) {
        return R.status(xiansuoChengbenLogService.save(xiansuoChengbenLog));
    }

    /**
     * 修改线索成本记录
     */
    @PreAuthorize("@ss.hasPermi('biz:bizXiansuoChengbenLog:edit')")
    @Log(title = "线索成本记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated XiansuoChengbenLog xiansuoChengbenLog) {
        return R.status(xiansuoChengbenLogService.save(xiansuoChengbenLog));
    }


    /**
     * 删除线索成本记录
     */
    @PreAuthorize("@ss.hasPermi('biz:bizXiansuoChengbenLog:remove')")
    @Log(title = "线索成本记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.status(xiansuoChengbenLogService.deleteXiansuoChengbenLogByIds(ids));
    }

    /**
     * 获取最大编号
     */
    @PreAuthorize("@ss.hasPermi('biz:bizXiansuoChengbenLog:query')")
    @GetMapping("/findMaxSort")
    public R findMaxSort() {
        return R.data(xiansuoChengbenLogService.findMaxSort(new XiansuoChengbenLog()));
    }

    /**
     * 导出线索成本记录列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizXiansuoChengbenLog:export')")
    @Log(title = "线索成本记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(XiansuoChengbenLog xiansuoChengbenLog) {

        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            xiansuoChengbenLog.setCompanyId(null);
        } else {
            xiansuoChengbenLog.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        List<XiansuoChengbenLog> list = xiansuoChengbenLogService.findList(xiansuoChengbenLog);
        ExcelUtil<XiansuoChengbenLog> util = new ExcelUtil<XiansuoChengbenLog>(XiansuoChengbenLog.class);
        return util.exportExcel(list, "线索成本记录数据");
    }

    /**
     * 统计广告成本
     *
     * @param xiansuoChengbenLog
     * @param request
     * @param response
     * @return
     */
    @GetMapping("/guanggaochengben")
    public R<PageInfo> guanggaochengben(XiansuoChengbenLog xiansuoChengbenLog, HttpServletRequest request, HttpServletResponse response) {

        xiansuoChengbenLog.setPage(new PageDomain(request, response));

        return R.data(xiansuoChengbenLogService.pageGuanggaoChengben(xiansuoChengbenLog));
    }

    /**
     * 统计广告成本合计
     *
     * @param xiansuoChengbenLog
     * @param request
     * @param response
     * @return
     */
    @GetMapping("/heJiGuanggaochengben")
    public R heJiGuanggaochengben(XiansuoChengbenLog xiansuoChengbenLog,
                                  HttpServletRequest request, HttpServletResponse response) {

        xiansuoChengbenLog.setPage(new PageDomain(request, response));

        return R.data(xiansuoChengbenLogService.hejiGuanggaochengben(xiansuoChengbenLog));
    }

    /**
     * 导出广告成本
     */
    @Log(title = "导出广告成本", businessType = BusinessType.EXPORT)
    @GetMapping("/exportChengBen")
    public R exportChengBen(XiansuoChengbenLog xiansuoChengbenLog) {
        List<GuanggaoChengBen> list = xiansuoChengbenLogService.guanggaoChengben(xiansuoChengbenLog);
        ExcelUtil<GuanggaoChengBen> util = new ExcelUtil<GuanggaoChengBen>(GuanggaoChengBen.class);
        return util.exportExcel(list, "广告成本统计");
    }
}
